package model

import (
	"log"
	"time"

	"gin-app/utils"

	_ "github.com/go-sql-driver/mysql"
	"xorm.io/xorm"

	"xorm.io/xorm/names"
)

// mysql
var DB *xorm.Engine
var err error

func init() {

	//DB, err = xorm.NewEngine("sqlite3", "./test.db")
	DB, err = xorm.NewEngine("mysql", utils.Url)
	if err != nil {

		log.Panic(err)
	}
	//defer engine.Close()
	DB.ShowWarn(true)

	DB.ShowSQL(utils.Show_sql)
	DB.SetMapper(names.SameMapper{})

	tbMapper := names.NewPrefixMapper(names.SameMapper{}, "t_")
	DB.SetTableMapper(tbMapper)

	DB.SetTZLocation(time.Local)

	//设置连接池的空闲数大小
	DB.SetMaxIdleConns(10)
	//设置最大打开连接数
	DB.SetMaxOpenConns(100)

	err = DB.Sync2(new(Users))

	if err != nil {
		log.Fatalln(err)
	}

	log.Println("db init")
}
